package com.xinlianshiye.im.utils.urlgoods;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Component
public class UrlGoodsUtils {

    public UrlGoodsUtils(RestTemplateBuilder restTemplateBuilder, UrlGoodsProperties urlGoodsProperties) {
        this.restTemplate = restTemplateBuilder.build();
        this.urlGoodsProperties = urlGoodsProperties;
    }

    private final RestTemplate restTemplate;
    private final UrlGoodsProperties urlGoodsProperties;

    public UrlGoods resolve(String urlString) {
        try {
            URL url = new URL(urlString);
            String host = url.getHost(); // 获取URL中域名
            String path = url.getPath(); // 获取域名后路径
            int port = url.getPort();
            if (port != -1) {
                host = host + ":" + port;
            }
            UrlGoods goods = null;
            if (urlGoodsProperties.getDomainGoodsUrlConfig().containsKey(host)) {
                UrlGoodsProperties.GoodsUrlConfig goodsUrlConfig = urlGoodsProperties.getDomainGoodsUrlConfig().get(host);
                List<UrlGoodsProperties.GoodsUrl> details = goodsUrlConfig.getDetails();
                String imageDomain = goodsUrlConfig.getImageDomain();
                for (UrlGoodsProperties.GoodsUrl detail : details) {
                    if (path.startsWith(detail.getPrefix())) {
                        String detailUrl = path.substring(detail.getPrefix().length());// 获取详情url
                        String shopId = detailUrl.substring(0, detailUrl.indexOf("."));// 获取商品ID 所在最后一个索引位置
                        if (isNumeric(shopId)) { // 判断这个商品ID是不是数字
                            Map map1 = getShopInfo(url.getProtocol() + "://" + url.getAuthority() + detail.getUrl() + shopId); // 请求对应的商品信息
                            if (map1.get("code").toString().equals("1")) {
                                Map<String, Object> result = (Map<String, Object>) map1.get("result");
                                goods = new UrlGoods();
                                goods.merge(result);
                                goods.setImageDomain(imageDomain);
                            }
                            break;
                        }
                    }
                }
            }
            if (goods == null) {
                Document document1 = Jsoup.parse(url, 30000);
                goods = new UrlGoods();
                goods.setName(document1.title());
            }
            goods.setUrl(urlString);
            return goods;
        } catch (IOException e) {
            //不是链接地址
            return null;
        }
    }

    public Map getShopInfo(String url) {
        return restTemplate.getForObject(url, Map.class);
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}
